package com.xsmart.oa.dao;

import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author 周志成
 */
@Component
public interface ShippingDAO {
    /**
     * 查询配送方式
     * @param page
     * @param shippingMap
     * @return
     */
    @Select("<script>select * from shipping where 1=1 <if test=\"shipping_code!=null and shipping_code!=''\">and shipping_code like concat('%',#{shipping_code},'%') </if><if test=\"shipping_name!=null and shipping_name!=''\"> and shipping_name like concat('%',#{shipping_name},'%') </if></script> ")
    List<Map<String,Object>> queryShipping(Pagination page, Map shippingMap);

    /**
     * 新增配送方式
     * @param shippingMap
     * @return
     */
    @Insert("insert into shipping " +
            "(shipping_code, " +
            "shipping_name," +
            "shipping_desc ,"+
            "insure,"+
            "support_cod,"+
            "enabled)" +
            "values (" +
            "#{shippingMap.shipping_code}," +
            "#{shippingMap.shipping_name}, " +
            "#{shippingMap.shipping_desc}," +
            "#{shippingMap.insure}," +
            "#{shippingMap.support_cod}," +
            "#{shippingMap.enabled})")
    int addShipping(@Param("shippingMap")Map shippingMap);

    /**
     * 修改配送方式
     * @param shippingMap
     * @return
     */
    @Update("update shipping " +
            "set " +
            "shipping_code = #{shippingMap.shipping_code}, " +
            "shipping_name = #{shippingMap.shipping_name}," +
            "shipping_desc = #{shippingMap.shipping_desc}," +
            "insure = #{shippingMap.insure}," +
            "support_cod = #{shippingMap.support_cod}," +
            "enabled = #{shippingMap.enabled} " +
            "where shipping_id = #{shippingMap.shipping_id}")
    int modShipping(@Param("shippingMap")Map shippingMap);

    /**
     * 删除配送方式
     * @param shippingId
     * @return
     */
    @Delete("DELETE FROM shipping WHERE shipping_id=#{shippingId}")
    int delShipping(Integer shippingId);

    /**删除运费模板
     * @param shippingTempletAttrId
     * @return
     */
    @Delete("DELETE FROM shipping_templet_attr WHERE shipping_templet_attr_id=#{shippingTempletAttrId}")
    int delShippingTempletAttr(Integer shippingTempletAttrId);


    /**修改配送模板状态
     * @param shippingTempletId
     * @return
     */
    @Delete("update shipping_templet set shipping_templet_status = #{shippingTempletStatus} WHERE shipping_templet_id=#{shippingTempletId}")
    int modSellerShippingStatus(@Param("shippingTempletId") Integer shippingTempletId,@Param("shippingTempletStatus") Integer shippingTempletStatus);



    /**查询配送模板
     * @param page
     * @param shippingMap
     * @return
     */
@Select("<script>" +
        "select ss.id,ss.ru_id,ss.shop_name,st.shipping_templet_name,st.shipping_title,st.templet_type,st.shipping_templet_id,shipping_templet_status \n" +
        "from shipping_templet st  \n" +
         "LEFT OUTER JOIN seller_shopinfo ss on ss.id = st.seller_shopinfo_id \n" +
        "LEFT OUTER JOIN infs_user iu on ss.ru_id = iu.user_id \n" +
        "where 1=1 " +
        "<if test=\"shopName!=null and shopName!=''\">and ss.shop_name like concat('%',#{shopName},'%') </if> " +
        "<if test=\"shippingTempletName!=null and shippingTempletName!=''\">and st.shipping_templet_name = #{shippingTempletName} </if> " +
        "GROUP BY ss.id " +
        "</script>")
    List<Map<String,Object>> querySeller(Pagination page, Map shippingMap);


    /**查询运费模板
     * @param page
     * @param shippingMap
     * @return
     */
@Select("<script>" +
        "select ss.id,s.shipping_name,sta.shipping_templet_attr_id,shipping_area_name,sta.shipping_area_ids,st.shipping_templet_id," +
        "(CASE WHEN sta.shipping_fee_type = '0' then '按重量计算' when  sta.shipping_fee_type = '1' then '按商品件数计算' END)shipping_fee_type ,\n" +
        "(shipping_first_fee*1)shipping_first_fee,(shipping_sec_fee*1)shipping_sec_fee,(shipping_free_fee*1)shipping_free_fee,(shipping_goods_fee*1)shipping_goods_fee " +
        "from shipping_templet_attr sta \n" +
        "LEFT OUTER JOIN shipping_templet st on sta.shipping_templet_id = st.shipping_templet_id \n" +
        "LEFT OUTER JOIN shipping s on sta.shipping_id = s.shipping_id \n" +
        "LEFT OUTER JOIN seller_shopinfo ss on ss.id = st.seller_shopinfo_id \n" +
        "where 1=1 " +
        "<if test=\"shopId!=null and shopId!=''\">and iu.id = #{shopId} </if> " +
        "<if test=\"shippingTempletId!=null and shippingTempletId!=''\">and st.shipping_templet_id = #{shippingTempletId} </if> " +
        "</script>")
    List<Map<String,Object>> queryFreightTemplate(Pagination page, Map shippingMap);


    /**根据编码查询地区名称
     * @param shippingAreaIds
     * @return
     */
    @Select("<script>select region_name from region where region_id in" +
            "<foreach collection=\"shippingAreaIds\" item=\"item\" index=\"index\" \n" +
            "open=\"(\" separator=\",\" close=\")\">#{item}" +
            "</foreach>" +
            "</script>")
    List<String>queryTemplateDesc(@Param("shippingAreaIds")String[]shippingAreaIds);


    /**查询配送方式ID
     * @param shippingName
     * @return
     */
    @Select("<script>" +
            "select shipping_id from shipping where shipping_name = #{shippingName} " +
            "</script>")
    List<Map<String,Object>>queryShippingId(@Param("shippingName")String shippingName);


    /**添加运费模板
     * @param shippingTempletMap
     * @return
     */
    @Insert("<script>" +
            "insert into shipping_templet_attr" +
            "(shipping_templet_id," +
            "shipping_id," +
            "shipping_area_name," +
            "shipping_fee_type," +
            "shipping_first_fee," +
            "shipping_sec_fee," +
            "shipping_free_fee," +
            "shipping_goods_fee," +
            "shipping_area_ids)" +
            "values" +
            "(#{shippingTempletMap.shippingTempletId}," +
            "#{shippingTempletMap.shippingId}," +
            "#{shippingTempletMap.shippingAreaName}," +
            "#{shippingTempletMap.shippingFeeType}," +
            "#{shippingTempletMap.shippingFirstFee}," +
            "#{shippingTempletMap.shippingSecFee}," +
            "#{shippingTempletMap.shippingFreeFee}," +
            "#{shippingTempletMap.shippingGoodsFee}," +
            "#{shippingTempletMap.shippingAreaIds})" +
            "</script>")
    int addFrightTemplet(@Param("shippingTempletMap")Map shippingTempletMap);


    /**查询省市县节点
     * @param parentId
     * @return
     */
    @Select("<script>SELECT region_id,parent_id,region_name,region_type FROM region WHERE <if test=\"parentId==null\">parent_id=1</if><if test=\"parentId!=null\">parent_id=#{parentId}</if></script>")
    List<Map<String, Object>> queryRegionParents(@Param("parentId") Long parentId);


    /**查询当前城市的父节点
     * @param regionId
     * @return
     */
    @Select("<script>SELECT parent_id FROM region WHERE region_id = #{regionId} </script>")
    List<Map<String, Object>> queryRegionParentId(@Param("regionId") Integer regionId);


    /**查询当前城市的名称
     * @param
     * @return
     */
    @Select("<script>SELECT region_id,region_name FROM region WHERE region_id = #{regionId} </script>")
    List<Map<String, Object>> queryRegionList(@Param("regionId")Integer regionId);

    /**通过地区名称查询地区ID
     * @param regionName
     * @return
     */
    @Select("<script>SELECT region_id FROM region WHERE region_name in " +
            "<foreach collection=\"regionName\" item=\"item\" index=\"index\" \n" +
            "open=\"(\" separator=\",\" close=\")\">#{item}" +
            "</foreach>" +
            "</script>")
    List<String> queryRegionId(@Param("regionName") List<String>regionName);


    /**修改配送模板
     * @param shippingTempletMap
     * @return
     */
    @Update("update shipping_templet " +
            "set " +
            "shipping_templet_name = #{shippingTempletMap.shippingTempletName}, " +
            "shipping_title = #{shippingTempletMap.shippingTitle} " +
            "where shipping_templet_id = #{shippingTempletMap.shippingTempletId}")
    int updateShippingTemplet(@Param("shippingTempletMap")Map shippingTempletMap);


    /**修改运费模板
     * @param FreightTempletMap
     * @return
     */
    @Update("update shipping_templet_attr set " +
            "shipping_area_name = #{shippingAreaName}, " +
            "shipping_fee_type = #{shippingFeeType},  " +
            "shipping_first_fee = #{shippingFirstFee},  " +
            "shipping_sec_fee = #{shippingSecFee},  " +
            "shipping_free_fee = #{shippingFreeFee},  " +
            "shipping_goods_fee = #{shippingGoodsFee}," +
            "shipping_area_ids = #{shippingAreaIds}  " +
            "where shipping_templet_attr_id = #{shippingTempletAttrId}")
            int modFreightTemplet(Map<String,Object> FreightTempletMap);
}

